//
//  JRBaseNav.swift
//  LED OTA
//
//  Created by rui on 2024/4/10.
//

import UIKit

open class JRBaseNav: UINavigationController {
    
    public var isEnabledInteractive: Bool = true
    
    private (set) var isInteractived: Bool = false

    public var navBarColor: UIColor = .white {
        willSet {
            navBarImage = UIImage.createImage(color: newValue)
        }
    }
    
    public var navBarImage: UIImage = UIImage.createImage(color: .white)

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.modalPresentationStyle = .fullScreen
        customInit()
    }
    
    public override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        self.modalPresentationStyle = rootViewController.modalPresentationStyle
        customInit()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        customInit()
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.isEnabledInteractive = true
        self.delegate = self;
    }
}

extension JRBaseNav {
    
    func customInit() {
        modalPresentationStyle = .fullScreen
    }
    
    func customNavigationBarInit() {
        if #available(iOS 13.0, *) {
            let navBarAppearance: UINavigationBarAppearance = UINavigationBarAppearance()
            navBarAppearance.shadowColor = .clear
            navBarAppearance.shadowImage = UIImage()
            navBarAppearance.backgroundColor = .clear
            navBarAppearance.backgroundImage = self.navBarImage
            self.navigationBar.scrollEdgeAppearance = navBarAppearance;
            self.navigationBar.standardAppearance = navBarAppearance
        } else {
            // Fallback on earlier versions
            self.navigationBar.backgroundColor = .clear
            self.navigationBar.setBackgroundImage(self.navBarImage, for: .default)
            self.navigationBar.shadowImage = UIImage()
        }

    }

    @objc
    func interactivePopToVC(gesture: UIGestureRecognizer) {
        switch gesture.state {
        case .began:
            self.isInteractived = true
            break
        case .changed:
            break
        case .ended, .cancelled, .failed:
            self.isInteractived = false
            break
        default:
            break
        }
    }
}

extension JRBaseNav: UINavigationControllerDelegate, UIGestureRecognizerDelegate {
    open func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let basicVC: JRBaseVC? = viewController as? JRBaseVC;
        basicVC?.completionBlock?()
        basicVC?.completionBlock = nil
        basicVC?.navigationController?.setNavigationBarHidden(basicVC?.isHiddenNavBar ?? false, animated: animated)
        self.navBarImage = basicVC?.navBarImage ?? UIImage.createImage(color: .white)
    }
    
    open func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        self.isInteractived = false
        if self.responds(to: #selector(getter: interactivePopGestureRecognizer)) {
            // 只有在二级页面生效
            if self.viewControllers.count >= 2 {
                if self.interactivePopGestureRecognizer?.delegate != nil && !self.interactivePopGestureRecognizer!.delegate!.isEqual(self) {
                    self.interactivePopGestureRecognizer?.delegate = self;
                    self.interactivePopGestureRecognizer?.addTarget(self, action: #selector(interactivePopToVC(gesture:)))
                }
            }
        } else if self.viewControllers.count == 1 && self.interactivePopGestureRecognizer?.delegate != nil {
            self.interactivePopGestureRecognizer?.delegate = nil
            self.interactivePopGestureRecognizer?.removeTarget(self, action: #selector(interactivePopToVC(gesture:)))
        }
        
        var isOn = true
        if viewController is JRBaseVC {
            let basicVC = viewController as! JRBaseVC
            isOn = basicVC.isEnabledInteractive
        }
        //开启滑动手势
        self.interactivePopGestureRecognizer?.isEnabled = self.isEnabledInteractive ? isOn : false;

    }
    
    open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.viewControllers.count == 1 {
            return false
        }
        return true
    }
    
    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

}
